package com.gsit.misn.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gsit.misn.domain.Misn;
import com.gsit.misn.domain.MisnLinks;
import com.gsit.misn.domain.User;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class MisnXmlProcesser<T> {
	
	static Logger logger = LoggerFactory.getLogger(MisnXmlProcesser.class);

	private final static String xmlLocation = "src\\main\\resources\\data\\";
	private final static String xmlFileName = "misn.xml";
	private InputStream inputStream;
	private OutputStream fileOut;
	private File file;
	private XStream xstream;

	public MisnXmlProcesser() {
		try {
			file = new File(xmlLocation + xmlFileName);
			if (file.isFile()) {
				logger.info("this is found ");
			} else {
				logger.info("File not found");
				file.createNewFile();
			}
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	
	
	public static void main(String[] args) {
		try {
			MisnXmlProcesser<Misn> processer = new MisnXmlProcesser<Misn>();
			
			
			Misn misn = (Misn)processer.reader();
			
			logger.info(misn.getMisnExcuter().getDescription());
			User user =  misn.getUser();
			logger.info("The user name is "+user.getName());
			MisnLinks links = misn.getMisnLinks();
			logger.info("Link "+links.getLoginUrl());
			
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	
	@SuppressWarnings("unused")
	private void dumyData(){
		
		/*User user = new User("aa", "aa", 0);
		Misn misn = new Misn();
		misn.setMisnExcuter(MisnExcuter.CREATE_FROM_XLS);
		MisnLinks misnLinks = new MisnLinks(); 
		misnLinks.setCreateBtnName("");
		misnLinks.setDeleteUrl("");
		misnLinks.setEditUrl("");
		misnLinks.setHomeUrl("");
		misnLinks.setLoginUrl("");
		misnLinks.setLogoutUrl("");
		misnLinks.setRecordCountBtnName("");
		misnLinks.setSearchUrl("");
		misnLinks.setSearchUrl("");
		misnLinks.setUpdateBtnName("");
		
		misn.setMisnLinks(misnLinks);
		
		List<User> users = new ArrayList<User>();
		users.add(user);
		user = new User("aa1", "aa1", 0);
		users.add(user);
		misn.setUsers(users);
		processer.write(misn);*/
		
	}
	
	@SuppressWarnings("unchecked")
	public T reader()throws IOException{
		inputStream = new FileInputStream(file);
		xstream = new XStream(new StaxDriver());
		xstream.autodetectAnnotations(true);
		xstream.alias("misn", Misn.class);
		
		return (T)xstream.fromXML(inputStream);
		
	}

	public void write(T t) throws IOException {

		fileOut = new FileOutputStream(file);
		xstream = new XStream(new StaxDriver());
		xstream.autodetectAnnotations(true);
		String xml = xstream.toXML(t);
		fileOut.write(xml.getBytes());
		fileOut.flush();

	}

}
